<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use COM\Upload;
/**
 * 行为控制器
 * @author huajie <banhuajie@163.com>
 */

class WechatController extends AdminController {

    public function index(){

        //查询数据
        $model=M('wgzh');
        if(IS_POST){
            //修改
            $arr=I('post.');
            $model->save($arr);
        }
        $arr=$model->find();
        $this->assign('arr',$arr);
        $this->display();
    }

//    关键字管理
    public function gjz(){
        $arr=M('wgjz')->select();
        $this->assign('arr',$arr);
        $this->display();
    }

//    关键字添加
    public function gjz_add(){
        if(IS_POST){
            $arr=array();
            $model=M('wgjz');
            if($_POST['type']=='2'){
//                dump($_POST);exit;
                //图片上传
                $list=array();
                $tw=array();
                foreach ($_FILES['tw_img']['name'] as $k=>$v){
                    $list[$k]['name']=$v;
                    $list[$k]['type']=$_FILES['tw_img']['type'][$k];
                    $list[$k]['tmp_name']=$_FILES['tw_img']['tmp_name'][$k];
                    $list[$k]['error']=$_FILES['tw_img']['error'][$k];
                    $list[$k]['size']=$_FILES['tw_img']['size'][$k];

                    $path=$this->file_upd($list[$k]);
                    $tw[$k]['Title']=$_POST['tw_title'][$k];
                    $tw[$k]['Description']=$_POST['tw_description'][$k];
                    $tw[$k]['PicUrl']=$_POST['tw_url'][$k];
                    $tw[$k]['Url']=$path;
                }
                $tw=json_encode($tw);

                $arr['rule_name']=$_POST['rule_name'];
                $arr['keyword']=$_POST['keyword'];
                $arr['type']=$_POST['type'];
                $arr['text']=$tw;
                $is=$model->add($arr);
            }
            else{
                $arr['rule_name']=$_POST['rule_name'];
                $arr['keyword']=$_POST['keyword'];
                $arr['type']=$_POST['type'];
                $arr['text']=$_POST['text'];
                $is=$model->add($arr);
            }
            if($is){
                $this->success('添加成功',U('gjz'));
            }else{
                $this->error('添加失败');
            }
            exit;
        }
        $this->display();
    }

    //图片上传
    public function file_upd($file){
        if($file['error']=='0'){
            $hz=strrchr($file['name'],'.'); //后缀
            $name=str_replace('.','t',microtime(true)).$hz;
            $path='/Public/img/'.$name;
            if(move_uploaded_file($file['tmp_name'],$_SERVER['DOCUMENT_ROOT'].$path)){
                return $path;
            }else{
                return false;
            }
        }else{
            return false;
        }

    }


    //自定义菜单
    public function zdycd(){
        $model=M('zdycd');
        $arr=$model->where("pid =0")->select();
        $list=array();
        foreach($arr as $v){
            $v['to']=$model->where("pid ={$v['id']}")->select();
            $list[]=$v;
        }


        $this->assign('arr',$list);
        $this->display();
    }

    public function zdycd_add(){
        if(IS_POST){
            //添加一级菜单
            $arr=array();
            $arr['name']=$_POST['name'];
            $arr['type']=$_POST['type'];
            $arr['content']=$_POST['content'];
            $arr['pid']=$_POST['pid']?$_POST['pid']:0;
            if(M('zdycd')->add($arr)){
                $this->success('添加成功',U('zdycd'));
            }else{
                $this->error('添加失败');
            }
            exit;
        }
        $this->display();
    }


    public function zdycd_edit(){
        if(IS_POST){
            //添加一级菜单
            $arr=array();
            $arr['name']=$_POST['name'];
            $arr['type']=$_POST['type'];
            $arr['content']=$_POST['content'];
            $arr['pid']=$_POST['pid']?$_POST['pid']:0;
            $arr['id']=$_POST['id'];
            if(M('zdycd')->save($arr)){
                $this->success('操作成功',U('zdycd'));
            }else{
                $this->error('操作失败');
            }
            exit;
        }
        $id=$_GET['id'];
        $model=M('zdycd');
        $arr=$model->find($id);
        $this->assign('arr',$arr);
        $this->display('zdycd_add');
    }

    public function zdycd_upd(){
       $arr=array();
        $arr['id']=$_POST['id'];
        $arr[$_POST['type']]=$_POST['vals'];
        if(M('zdycd')->save($arr)){
            $this->ajaxReturn(array('msg'=>'ok'));
        }else{
            $this->ajaxReturn(array('msg'=>'false'));
        }
    }


    public function zdycd_remove(){
        $id=$_GET['id'];
        $model=M('zdycd');
        $arr=$model->where("pid = $id")->find();
        if(!empty($arr)){
            $this->error('存在子菜单不能删除');
        }else{
            $model->delete($id);
            $this->success('操作成功',U('zdycd'));
        }

    }

    //生成自定义菜单
    public function zdycd_sc(){
        //查询自定义菜单数据----生成自定义菜单
        $wechat=M('wgzh')->find(); //公众号凭证

        $model=M('zdycd');

        $boot=$model->where("pid = 0")->select();
        $list=array();
        foreach($boot as $v){
            $v['arr']=$model->where("pid = '{$v['id']}'")->select();
            $list[]=$v;
        }

        //拼接数据
        $arr=array();
        foreach ($list as $v){
            $cd=array();
            $cd['name']= urlencode($v['name']);
            if($v['type']=='view'){
                $cd['type']=$v['type'];
                $cd['url']=$v['content'];
            }else if($v['type']=='click'){
                $cd['type']=$v['type'];
                $cd['key']=$v['content'];
            }else if($v['type']=='sub_button'){
                //有子菜单，需要拼接子菜单
                $cd['sub_button']=array();
                foreach ($v['arr'] as $j){
                    $zcd=array();
                    $zcd['type']=$j['type'];
                    $zcd['name']=urlencode($j['name']);
                    if($j['type']=='view'){
                        $zcd['url']=$j['content'];
                    }else if($j['type']=='click'){
                        $zcd['key']=$j['content'];
                    }
                    $cd['sub_button'][]=$zcd;
                }

            }

            $arr['button'][]=$cd;
        }

        $data=urldecode(json_encode($arr));
//        echo $data;exit;

        //请求接口
        $appid=$wechat['appid'];
        $appsercret=$wechat['secret'];
        $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsercret";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $result = curl_exec($ch);
        curl_close ($ch);
        $arr=json_decode($result,true);
        $access_token=$arr['access_token']; //得到access_token


        //自定义菜单接口
        $url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=$access_token";
        $ch=curl_init();

        curl_setopt($ch,CURLOPT_URL,$url);//请求地址
        curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'POST');//请求地址
        curl_setopt($ch,CURLOPT_POSTFIELDS,$data);//发送数据
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//采集数据不输出
        //https curl请求需要加上配置
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        $result=curl_exec($ch);
        if($result==false){
            echo curl_error($ch);exit;
        }

        $result=json_decode($result,true);

        if($result['errcode']=='0'){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }


    }
}
